<template>
  <div class="dashboard-container">
    <el-row v-if="list.length>0" :gutter="20" style="margin-bottom: 20px">
      <el-col v-for="(item, index) in list" :key="index" :span="6">
        <el-tooltip placement="top">
          <div slot="content">
            <div class="tooltip-content">
              <table>
                <tr>
                  <td>{{ `${item.cilpIncome}：${item.filmEditi} 元;` }}</td>
                  <td>{{ `${item.moveIncome}：${item.moveBrick} 元;` }}</td>
                </tr>
                <tr>
                  <td>{{ `${item.fundIncome}：${item.fundPrice} 元;` }}</td>
                  <td>{{ `${item.capitalIncome}：${item.capitalPrice} 元;` }}</td>
                </tr>
              </table>
            </div>
          </div>
          <div class="card-panel">
            <div class="card-panel-icon-wrapper" :class="getIconClass(index)">
              <svg-icon :icon-class="getIconName(index)" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">
                {{ item.agencyName }}
              </div>
              <count-to
                :start-val="0"
                :end-val="item.amount"
                :duration="getDuration(index)"
                class="card-panel-num"
                :decimals="2"
              />
            </div>
          </div>
        </el-tooltip>
      </el-col>
    </el-row>
    <!-- 每年 -->
    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom: 20px">
      <div class="chat_line">
        <div style="color:#008acd">金额趋势</div>
        <div class="chat_lines">
          <el-select
            v-model="temp.amountTrend"
            placeholder="请选择"
            @change="getBarLine"
          >
            <el-option
              v-for="item in amountType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-radio-group
            v-model="temp.isline"
            style="display: flex;"
            size="mini"
            @change="getLine"
          >
            <el-radio-button :label="false">今月</el-radio-button>
            <el-radio-button :label="true">今年</el-radio-button>
          </el-radio-group>
        </div>
      </div>
      <line-chart :chart-data="lineChartData" />
    </el-row>

    <el-row :gutter="32">
      <!-- 每周 -->
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <div style="height: 30px;">
            <div style="color:#008acd">本周金额</div>
          </div>
          <bar-chart :chart-data="barChartData" />
        </div>
      </el-col>
      <!-- 占比 -->
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <div class="chat_line" style="height: 30px;">
            <div style="color:#008acd">项目占比</div>
            <el-radio-group
              v-model="temp.ispie"
              style="display: flex;"
              size="mini"
              @change="getPieProportion"
            >
              <el-radio-button :label="false">支出</el-radio-button>
              <el-radio-button :label="true">收入</el-radio-button>
            </el-radio-group>
          </div>
          <pie-chart :chart-data="pieChartData" />
        </div>
      </el-col>
      <!-- 日志 -->
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <div class="log">
            <div style="color:#008acd">
              <span>代码日志</span>
              <span style="float: right;cursor: pointer;" @click="updateLog">更多</span>
            </div>
            <div v-for="(item,index) in logList" :key="index" class="log_chat">
              <img class="imgs" :src="item.url" alt="">
              <el-link
                :underline="false"
                @click="viewCode(item.date)"
              >
                <span>{{ item.message }}</span>
              </el-link>
              <span>{{ item.date }}</span>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <logDialog
      :visible.sync="logVisble"
      :data="logDate"
      :data-time="dataTime"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getBasisList } from '@/api/basic-data'
import { getList } from '@/api/dashboard'
import { convertDate } from '@/utils/index'
import { getConstantByType } from '@/utils/index'
import portApi from '@/api/outerNet'
import LineChart from './components/LineChart'
import PieChart from './components/PieChart'
import BarChart from './components/BarChart'
import LogDialog from './components/LogDialog'
import CountTo from 'vue-count-to'

export default {
  name: 'Dashboard',
  components: {
    CountTo,
    LineChart,
    PieChart,
    BarChart,
    LogDialog
  },
  data() {
    return {
      temp: {
        amountTrend: '01',
        isline: false,
        ispie: false
      },
      list: [],
      logList: [],
      logDate: [],
      constants: [],
      amountType: [],
      logVisble: false,
      dataTime: '',
      lineChartData: {},
      barChartData: {},
      pieChartData: {}
    }
  },
  computed: {
    ...mapGetters(['name'])
  },
  mounted() {
    this.getList()
    this.$nextTick(() => {
      this.$bus.$on('changeNode', (val) => {
        if (val === 'dashboard') {
          this.getList()
        }
      })
    })
  },
  methods: {
    getList() {
      this.getPrice()
      this.getLine()
      this.getConstant()
      this.getAmountProportion()
      this.getPieProportion()
      this.getLog()
    },
    // 代办
    async getPrice() {
      const result = await getList('getTaskToDo')
      if (result.code === 200) {
        this.list = JSON.parse(JSON.stringify(result.data.pageItems))
      }
    },
    // 金额趋势
    async getLine() {
      const params = {
        sign: this.temp.isline,
        type: this.temp.amountTrend
      }
      const result = await getList('getMonetaryTrend', params)
      if (result.code === 200) {
        this.lineChartData = JSON.parse(JSON.stringify(result.data.pageItems[0]))
      }
    },
    // 本周金额
    async getAmountProportion() {
      const params = {
        type: this.temp.amountTrend
      }
      const result = await getList('getAmountChat', params)
      if (result.code === 200) {
        this.barChartData = JSON.parse(JSON.stringify(result.data.pageItems[0]))
      }
    },
    // 项目占比
    async getPieProportion() {
      const params = {
        sign: this.temp.ispie,
        type: this.temp.amountTrend
      }
      const result = await getList('getPieChat', params)
      if (result.code === 200) {
        this.pieChartData = JSON.parse(JSON.stringify(result.data.pageItems[0]))
      }
    },
    // 常量
    async getConstant() {
      const result = await getBasisList('constant')
      if (result.code === 200) {
        this.constants = JSON.parse(JSON.stringify(result.data.pageItems))
        this.amountType = await getConstantByType(this.constants, 2)
      }
    },
    // 日志
    async getLog() {
      portApi.getLog().then(res => {
        const targetObjects = res.data.map(item => {
          return {
            'id': item.author.id,
            'url': item.author.avatar_url,
            'date': convertDate(item.commit.committer.date),
            'message': item.commit.message.replace(/\n/g, '')
          }
        })
        this.logList = targetObjects.slice(0, 5)
        this.logDate = targetObjects
      }).catch(error => {
        console.error(error)
      })
    },
    async getBarLine() {
      this.getLine()
      this.getPieProportion()
      this.getAmountProportion()
    },
    viewCode(val) {
      this.dataTime = val
      this.logVisble = true
    },
    updateLog() {
      this.logVisble = true
      this.dataTime = this.logList[0].data
    },
    getIconClass(index) {
      // 根据index返回不同的类名
      const iconNames = ['icon-project', 'icon-price', 'icon-money', 'icon-cost']
      return iconNames[index % iconNames.length]
    },
    getIconName(index) {
      // 根据index返回不同的图标类名
      const iconNames = ['project', 'price', 'money', 'cost']
      return iconNames[index % iconNames.length]
    },
    getDuration(index) {
      // 根据index返回不同的动画持续时间
      return index % 2 === 0 ? 2600 : 3000
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 20px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-project {
        background: #40c9c6;
      }

      .icon-price {
        background: #f4516c;
      }

      .icon-money {
        background: #fd9c55;
      }

      .icon-cost {
        background: #34bfa3
      }
    }

    .icon-project {
      color: #40c9c6;
    }

    .icon-price {
      color: #f4516c;
    }

    .icon-money {
      color: #fd9c55;
    }

    .icon-cost {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 10px 0 0 14px;
      padding: 12px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
  }

  .chat_line{
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .tooltip-content{
    display: flex;
    flex-direction: row;
    align-items: space-between;
  }

  .chat_lines{
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    align-items: center;
  }
 }
}

.log{
  height:330px;
  padding: 0 10px 10px 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  .log_chat{
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-direction: row;
    font-size: 14px;
  }
  .imgs{
    width: 35px;
    height: 35px;
    margin-left: 15px;
    border-radius: 50%;
  }
}

::v-deep .el-tooltip__popper {
  font-size: 14px !important; /* 设置字体大小 */
  color: #ff0000 !important; /* 设置字体颜色 */
  background-color: #f0f0f0 !important; /* 设置背景颜色 */
  /* 其他样式设置 */
}
::v-deep .el-select {
  width: 35% !important;
  margin-right: 3%;
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
